package Class.Dialog;

import Class.Handler.*;
import Class.Dialog.*;
import Class.TextWindow.*;
import Class.Main.*;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

public class FindDialog extends JDialog implements ActionListener {

    private TextWindow textWindow;
    private JTextField findField = new JTextField();
    private JButton virtualFindFieldButton = new JButton();
    private JCheckBox caseSensitiveBox = new JCheckBox("区分大小写(C)");
    private JCheckBox loopBox = new JCheckBox("循环查找(R)");
    private JButton findNextButton = new JButton("查找下一个(F)");
    private JButton cancelButton = new JButton("取消");

    public FindDialog(TextWindow textWindow){
        super(textWindow,"查找",true);
        this.textWindow=textWindow;

        //设置助记符
        caseSensitiveBox.setMnemonic(KeyEvent.VK_C);
        loopBox.setMnemonic(KeyEvent.VK_R);
        findNextButton.setMnemonic(KeyEvent.VK_F);
        virtualFindFieldButton.setMnemonic(KeyEvent.VK_N);



        //建立框体，使用绝对布局，需指定坐标
        JPanel jPanel = new JPanel(null);
        JLabel findLabel = new JLabel("查找内容(N):");

        //添加组件
        jPanel.add(virtualFindFieldButton);
        jPanel.add(findLabel);
        jPanel.add(findField);
        jPanel.add(caseSensitiveBox);
        jPanel.add(loopBox);
        jPanel.add(findNextButton);
        jPanel.add(cancelButton);

        //对组件布局
        findLabel.setBounds(30,20,80,30);

        findField.setBounds(100,20,280,30);

        findNextButton.addActionListener(this);
        findNextButton.setBounds(395,15,115,35);

        cancelButton.addActionListener(this);
        cancelButton.setBounds(395,65,115,35);

        caseSensitiveBox.addActionListener(this);
        caseSensitiveBox.setBounds(40,55,115,20);

        loopBox.addActionListener(this);
        loopBox.setBounds(40,85,115,20);

        virtualFindFieldButton.addActionListener(this);

        this.getContentPane().add(jPanel);
        setSize(550,180);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //不用actionComman的另一种方法,getSource(),判断谁被点击
        if(e.getSource() == cancelButton){
            this.dispose();
        } else if (e.getSource() == findNextButton) {
            String findText = findField.getText();
            if(StringUtils.isBlank(findText)){
                return;
            }
            int caretPosition = textWindow.getTextArea().getCaretPosition();
            doFind(findText,caretPosition,false);

        } else if (e.getSource() == virtualFindFieldButton) {
//            System.out.println("virtualButton is ready");
            findField.requestFocusInWindow();
        }
    }

    //findText->要搜索的内容，fromIndex->从何处开始查找的索引
    private void doFind(String findText, int fromIndex,boolean isFromLoop) {
        //方法思路：首先获取到文本输入区域的内容和查找内容字符串，然后两者匹配
        String content = textWindow.getTextArea().getText();
        if(StringUtils.isBlank(content)){
            return;
        }
        int index;
        //从fromIndex后往后查找findText的内容
        if(caseSensitiveBox.isSelected()){
            index = content.indexOf(findText,fromIndex);
        }else {
            index = StringUtils.indexOfIgnoreCase(content,findText,fromIndex);
        }
        if(index>-1){
            //搜索到了content，选中content内容
            textWindow.getTextArea().select(index,index+findText.length());
        }else {//fromIndex后面没有内容，要往前搜
            if(loopBox.isSelected() && !isFromLoop ){
                System.out.println("递归调用");
                doFind(findText,0,true);//递归调用
            }else {
                JOptionPane.showMessageDialog(this,"找不到\""+findText+"\"内容");
            }
        }
    }
}
